require 'libs/gesture'
require 'libs/fonts'
require 'libs/smartClip'
require 'widgets/title'
require 'widgets/inputCode'
require 'widgets/keyboard'
require 'widgets/slider'
require 'widgets/launchButton'

function love.load()
    -- preload some stuffs
    x, y, w, h = love.window.getSafeArea( )
end

function love.draw()
    title.draw()
    inputCode.draw()
    keyboard.draw()
    launchButton.draw()
end

function love.update( dt )
    -- operate over game state 
    x, y, w, h = love.window.getSafeArea( )
    gesture.update(dt)
    keyboard.update(dt)
    launchButton.update(dt)
    smartClip.update(dt)
    slider.update(dt)
end

function launchButton.onTouch()
    love.system.openURL('https://nhentai.net/g/'..inputCode:getCode())
end

function keyboard.onPress(digit)
    if digit then
        inputCode.addDigit(digit)
    else
        inputCode.removeDigit()
    end
end

function smartClip.onCode(code)
    inputCode.setCode(code)
end

function slider.setup(setPosition, setSize, add)
    setPosition(x, h-keyboard.getHeight()+y) 
    setSize(w, keyboard.getHeight())
    add(function (x, y, w, h)
        launchButton.setPosition(x+w/2-launchButton.getWidth()/2, y+h/2-launchButton.getHeight()/2)    
    end)
    add(function (x, y, w, h)
        keyboard.setPosition(x+w/2-keyboard.getWidth()/2, y+h/2-keyboard.getHeight()/2)
    end)
end
